package com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Response;

/**
 * Created by Administrator on 2018/5/25.
 */

public class JsonUitl {
    public static Gson mGson = new Gson();

    /**
     * 将json字符串转化成实体对象
     * @param json
     * @param classOfT
     * @return
     */
    public static Object stringToObject( String json , Class classOfT){
        return  mGson.fromJson( json , classOfT ) ;
    }

    /**
     * 将对象准换为json字符串 或者 把list 转化成json
     * @param object
     * @param <T>
     * @return
     */
    public static <T> String objectToString(T object) {
        return mGson.toJson(object);
    }

    /**
     * 把json 字符串转化成list
     * @param json
     * @param cls
     * @param <T>
     * @return
     */
    public static <T> List<T> stringToList(String json , Class<T> cls  ){
        Gson gson = new Gson();
        List<T> list = new ArrayList<T>();
        JsonArray array = new JsonParser().parse(json).getAsJsonArray();
        for(final JsonElement elem : array){
            list.add(gson.fromJson(elem, cls));
        }
        return list ;
    }
    /**
     * @desc 解析date 字段
     * */
    @Nullable
    public static String getStringDateFeld(JSONObject jsonObject)  {
        try {
            if(jsonObject.has("data")){
                String responseDate = jsonObject.getString("data");
                if(responseDate != null && !TextUtils.isEmpty(responseDate)){
                    return responseDate;
                }else{
                    return "";
                }
            }
        } catch (JSONException e) {
            Log.e("SplashPersenter", "NetworkResponseDateDeal:  ============== e " +e);
        }
        return "";
    }

    /**
     * @desc  解析Response
     * */
    public static String NetWorkResponse(Response response){
        String NetWorkResponse = null;
        try {
            NetWorkResponse = response.body().string();
        } catch (IOException e) {
            Log.e(":CloudListPersenter", " 解析Response : "+e);
        }
        return NetWorkResponse;
    }

    // 解析json对象字符串
    public static JSONObject NetworkResponseDateDeal(String NetWorkResponse) {
        try {
            JSONObject jsonObject = new JSONObject(NetWorkResponse);
            return jsonObject;
        } catch (Exception e) {
            Log.e(":CloudListPersenter", "JSONObject  : "+e);
        }
        return null;
    }
    /**
     * @desc 解析code
     * */
    public static int getParsinCode(JSONObject jsonObject){
        try {
            if(jsonObject.has("code")){
                return  jsonObject.getInt("code");
            }
        } catch (JSONException e) {}
        return 0;
    }

    /**
     * @desc 转换数据 jaon 对象
     * */
    public static Object formDate(JSONObject jsonObject, Class classOfT){
        String normalDate = getStringDateFeld(jsonObject);
        return mGson.fromJson(normalDate,classOfT);
    }

}
